package com.kantboot.functional.email.service.service;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.kantboot.functional.common.message.domain.dto.MessageDTO;
import com.kantboot.functional.common.message.enums.MessageStatusCodeEnum;
import com.kantboot.functional.email.domain.entity.FunctionalEmail;
import com.kantboot.functional.email.repository.FunctionalEmailRepository;
import com.kantboot.functional.email.service.IFunctionalEmailService;
import com.kantboot.functional.email.setting.FunctionalEmailSetting;
import com.kantboot.global.exception.GlobalException;
import com.kantboot.util.common.exception.BaseException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class FunctionalEmailServiceImpl implements IFunctionalEmailService {

    @Resource
    private FunctionalEmailRepository repository;

    @Resource
    private FunctionalEmailSetting setting;

    @Async
    @Override
    public void send(MessageDTO messageDTO) {
        FunctionalEmail functionalEmail = getFunctionalEmail(messageDTO);
        // 保存消息
        FunctionalEmail save = repository.save(functionalEmail);

        MailAccount mailAccount = setting.getMailAccount();
        try {
            MailUtil.send(mailAccount, save.getTo(), save.getSubject(), save.getContent(), save.getIsHtml());
        } catch (Exception e) {
            e.printStackTrace();
            // 异常信息
            BaseException emailServerError = GlobalException.functionalEmail.EMAIL_SERVER_ERROR;
            // 发送失败，更新消息状态
            save.setMessageStatusCode(MessageStatusCodeEnum.STATUS_CODE_FAIL);
            // 设置失败原因编码
            save.setMessageStatusFailReasonCode(emailServerError.getStateCode());
            // 设置失败原因
            save.setMessageStatusFailReason(emailServerError.getMessage());
            // 保存失败消息
            repository.save(save);
            // 抛出异常，提示发送失败
            throw emailServerError;
        }
        save.setMessageStatusCode(MessageStatusCodeEnum.STATUS_CODE_SUCCESS);
        repository.save(save);
    }

    @NotNull
    private static FunctionalEmail getFunctionalEmail(MessageDTO messageDTO) {
        FunctionalEmail functionalEmail = new FunctionalEmail();
        // 设置状态编码
        functionalEmail.setMessageTypeCode(messageDTO.getMessageTypeCode());
        // 设置接收者
        functionalEmail.setTo(messageDTO.getTo());
        // 设置主题
        functionalEmail.setSubject(messageDTO.getSubject());
        // 设置内容
        functionalEmail.setContent(messageDTO.getContent());
        Boolean isHtml = messageDTO.getIsHtml();
        if (isHtml == null) {
            isHtml = false;
        }
        // 设置是否是html
        functionalEmail.setIsHtml(isHtml);
        // 设置消息状态
        functionalEmail.setMessageStatusCode(MessageStatusCodeEnum.STATUS_CODE_SENDING);
        return functionalEmail;
    }

}